package com.partybuilding.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.partybuilding.domain.entity.AdminRole;
import com.partybuilding.domain.entity.Admintable;
import com.partybuilding.domain.entity.LoginUser;
import com.partybuilding.domain.entity.Roletable;
import com.partybuilding.enums.DataScopeEnum;
import com.partybuilding.mapper.AdminRoleMapper;
import com.partybuilding.mapper.AdmintableMapper;
import com.partybuilding.service.AdminRoleService;
import com.partybuilding.service.RoletableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author wulala
 * 重写自带的loadUserByUsername方法
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private AdmintableMapper admintableMapper;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{

        //根据用户名查询用户
        LambdaQueryWrapper<Admintable> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Admintable::getUser_name,username);
        Admintable admintable = admintableMapper.selectOne(queryWrapper);
        //判断是否查询到用户，抛出异常并处理
        if(Objects.isNull(admintable)){
            throw new RuntimeException("用户不存在");
        }
        List<String> list=new ArrayList<>(Arrays.asList("null"));
        return new LoginUser(admintable,list,"10" );
    }
}
